package com.srxz.mobilepharmacy.util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "sessionfilter",urlPatterns = "/mph/*")
public class SessionAspect implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       // System.out.println("-----检校session开始-----");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        if (session.getAttribute("user") == null || session.getAttribute("userhouse") == null){
         //   System.out.println("过期sessionId:"+session.getId());
            ((HttpServletResponse)servletResponse).sendRedirect(request.getContextPath()+"/timeout");
        }else{
         //   System.out.println(session.getAttribute("userhouse"));
            filterChain.doFilter(servletRequest, servletResponse);
        }
       // System.out.println("-----检校session结束-----");
    }

    @Override
    public void destroy() {

    }
}
